package com.example.teamtribe.main.club.clubintroduce.media.photo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/6/17.
 */

public class JiaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private List<LocalMedia> mediaList;

    public JiaAdapter(){
        mediaList = new ArrayList<>();
    }
        public void addMedia(LocalMedia media) {
        mediaList.add(media);
    }
    public void setList(List<LocalMedia> medias) {
        mediaList=medias;
    }
    public void clear(){
        if(mediaList!=null)
            mediaList.clear();
    }
    static class MediaViewHolder extends RecyclerView.ViewHolder {
        ImageView cover;

        public MediaViewHolder(View itemView) {
            super(itemView);
            cover = itemView.findViewById(R.id.iv_photo);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        context = parent.getContext();
        final RecyclerView.ViewHolder holder;
        final View view = LayoutInflater.from(context).inflate(R.layout.item_club_photo, parent, false);
        holder = new MediaViewHolder(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position<3){
            switch(position) {
                case 1:Glide.with(context).load(R.drawable.two).into(((MediaViewHolder) holder).cover);
                    break;
                case 2:Glide.with(context).load(R.drawable.three).into(((MediaViewHolder) holder).cover);
                    break;
                default:Glide.with(context).load(R.drawable.four).into(((MediaViewHolder) holder).cover);
                break;
            }

        }else {
            LocalMedia media = mediaList.get(position-3);
            File file = new File(media.getPath());
            Glide.with(context).load(file).into(((MediaViewHolder) holder).cover);
        }
    }

    @Override
    public int getItemCount() {
        return mediaList.size()+3;
    }
}
